package models.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;

/**
 * 用户个人信息
 * @author zhangpeng
 *
 */

@Entity
@Table(name="person_user_info")
public class PersonUserInfo extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String username;
	/**
	 * 证件类型
	 */
	public String cardtype;
	/**
	 * 证件号 
	 */
	public String cardid;
	/**
	 * 邮寄地址
	 */
	public String post_addr;
	/**
	 * 邮编 
	 */
	public String post_code;
	/**
	 * 个人主页
	 */
	public String web_url;
	/**
	 * 自我介绍 	
	 */
	public String self_desc;
	/**
	 * 兴趣爱好 
	 */
	public String interest;
	
	public static PersonUserInfo getInstances(){
		return new PersonUserInfo();
	}
	
	public static Model.Finder<Long, PersonUserInfo> find = new Model.Finder<Long, PersonUserInfo>(Long.class, PersonUserInfo.class);
	
	/**
	 * 创建用户个人信息
	 * 
	 * @param psui
	 * @return
	 * @throws MengException
	 */
	public boolean createPSUI(PersonUserInfo psui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(psui.username)) {
			throw new MengException("700001", "用户名不存在");
		}

		if (verifyPersonUserInfo(psui.username)) {
			throw new MengException("700002", "用户个人信息已存在");
		}

		try {
			psui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createPSUI Exception," + t.getMessage());
			throw new MengException("700003", "用户个人信息保存失败");
		}
	}

	public boolean createPSUI(UserInfo ui) throws MengException {

		if (verifyPersonUserInfo(ui.username)) {
			throw new MengException("700008", "用户基本信息已存在");
		}
		PersonUserInfo psui = getInstances();
		psui.username = ui.username;
		try {
			psui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("700009", "用户基本信息保存失败");
		}
	}

	/**
	 * 用户个人信息修改
	 * 
	 * @param psui
	 * @return
	 * @throws MengException
	 */
	public boolean modifyPSUI(PersonUserInfo psui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(psui.username)) {
			throw new MengException("700004", "用户名不存在");
		}
		psui.id = getPSUI(psui.username).id;

		try {
			psui.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyPSUI Exception," + t.getMessage());
			throw new MengException("700005", "用户个人信息修改失败");
		}
	}

	/**
	 * 获取用户个人信息
	 * 
	 * @param username
	 * @return
	 * @throws MengException
	 */
	public PersonUserInfo getPSUI(String username) throws MengException {

		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("700006", "用户名不存在");
		}

		try {

			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getPSUI Exception," + t.getMessage());
			throw new MengException("700007", "获取用户个人信息失败");
		}
	}
	
	public boolean verifyPersonUserInfo(String username) throws MengException {

		if (getPSUI(username) != null) {
			return true;
		}

		return false;
	}

}
